import { Pipe, PipeTransform } from "@angular/core";

/**
 * 一个去除扩展名或某些固定匹配的Pipe；
 * 
 * @export
 * @class StripExtensionPipe
 * @implements {PipeTransform}
 */
@Pipe({
  name: "stripExtension",
})
export class StripExtensionPipe implements PipeTransform {
  
  /**
   * 返回Value中去掉以Symbol为分隔符其后的部分，保留dotPos个分隔符
   * 
   * @param {string} value
   * @param {number} [skip=0]
   * @param {string} [symbol="."]
   * @returns
   * 
   * @memberOf StripExtensionPipe
   */
  transform(value: string, skip = 0, symbol = ".") {
    if (!value || !value.length) return value;
    return value.slice(0, Array.prototype.map.call(value, (v, i) => {
      return v === symbol ? i : -1;
    }).filter(pos => pos !== -1)[skip]);
  }
}
